package com.system.xingzhi.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.xingzhi.dao.mapper.ComponentMapper;
import com.system.xingzhi.model.ReturnData;
import com.system.xingzhi.model.Component;
import com.system.xingzhi.service.ComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ComponentServiceImpl implements ComponentService {

    @Autowired
    ComponentMapper componentMapper;

    @Override
    public ReturnData deleteByPrimaryKey(String componentId) {
        ReturnData returnData=new ReturnData();
        try {
            componentMapper.deleteByPrimaryKey(componentId);
            returnData.setCode(0);
            returnData.setDetail("删除组件成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("删除组件失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData insert(Component record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(componentMapper.insert(record));
            returnData.setCode(0);
            returnData.setDetail("添加组件成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("添加组件失败");
            return returnData;
        }
        return returnData;

    }

    @Override
    public int insertSelective(Component record) {
        return componentMapper.insertSelective(record);
    }

    @Override
    public Component selectByPrimaryKey(String componentId) {
        return componentMapper.selectByPrimaryKey(componentId);
    }

    @Override
    public int updateByPrimaryKeySelective(Component record) {
        return componentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ReturnData updateByPrimaryKey(Component record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(componentMapper.updateByPrimaryKey(record));
            returnData.setCode(0);
            returnData.setDetail("更新组件组件成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("更新组件失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData selectByParameter(Map<String, String> map,Integer pageNum, Integer pageSize) {
        ReturnData returnData=new ReturnData();
        try {
            //分页
            PageHelper.startPage(pageNum, pageSize);

            List<Component> queryResult = componentMapper.selectByParameter(map);
            returnData.setData(new PageInfo<>(queryResult));
            returnData.setCode(0);
            returnData.setDetail("查询组件数据成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("查询组件失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData getAllComponent() {
        ReturnData returnData=new ReturnData();
        try {
            List<Component> queryResult = componentMapper.getAllComponent();
            returnData.setData(new PageInfo<>(queryResult));
            returnData.setCode(0);
            returnData.setDetail("获取所有组件成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("获取所有组件失败");
            return returnData;
        }
        return returnData;
    }

}
